package com.ryskoo.catering.consumestatistics.service;

import com.ryskoo.catering.consumestatistics.domain.ConsumeStatisticsPO;
import com.ryskoo.catering.consumestatistics.repository.IConsumeStatisticsRepository;
import com.ryskoo.framework.core.service.BasicServiceImpl;
import com.ryskoo.framework.core.utils.DoubleFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @author BeHappy
 */
@Service
public class ConsumeStatisticsServiceImpl extends BasicServiceImpl<ConsumeStatisticsPO> implements IConsumeStatisticsService {
    @Autowired
    private IConsumeStatisticsRepository consumeStatisticsRepository;

    @PostConstruct
    public void init() {
        this.setDao(consumeStatisticsRepository);
    }


    @Override
    public ConsumeStatisticsPO getByVendorAndCustomer(String vendorUuid, String customerUuid) {
        return consumeStatisticsRepository.getByVendorAndCustomer(vendorUuid, customerUuid);
    }

    @Override
    public void create(String vendorUuid, String customerUuid, double payPrice) {
        ConsumeStatisticsPO po = this.getByVendorAndCustomer(vendorUuid, customerUuid);
        if(po == null){
            po = new ConsumeStatisticsPO();
            po.setVendorUuid(vendorUuid);
            po.setCustomerUuid(customerUuid);
            po.setTime(1);
            po.setMoney(payPrice);
            this.create(po);
        } else {
            po.setTime(po.getTime() + 1);
            po.setMoney(DoubleFormatUtil.add(po.getMoney(), payPrice));
            this.update(po);
        }
    }
}
